// @ts-nocheck
import { app } from '..';
import { GET, POST } from 'fastify-decorators';
import { Schema, z, ZodObject } from 'zod';
import { FastifyReply, FastifyRequest } from 'fastify';
import { PrismaClient, Prisma } from '@prisma/client';

export abstract class Base<
    T extends keyof Prisma.GlobalOmitConfig,
    U extends z.ZodRawShape
> {
    static private PaginationSchema = z.object({
        pageNum: z.number().default(1),
        pageSize: z.number().default(10),
    });

    protected abstract model(): T;
    protected abstract createSchema(): z.ZodObject<U>

    async create(request: FastifyRequest, reply: FastifyReply) {
        const data = this.createSchema().parse(request.body);
        return app.prisma[this.model()].create({ data });
    }

    async get(request: FastifyRequest, reply: FastifyReply) {
        const { pageNum, pageSize } = Base.PaginationSchema.parse(request.query);
        return app.prisma[this.model()].findMany({
            skip: pageNum * pageSize,
            take: pageSize,
        });
    }

    async query(request: FastifyRequest, reply: FastifyReply) {
        const { id } = z.object({ id: z.string() }).parse(request.params);
        return app.prisma[this.model()].findFirst({ where: { id } });
    }

    async update(request: FastifyRequest, reply: FastifyReply) {
        const { id } = z.object({ id: z.string() }).parse(request.params);
        const data = this.createSchema().partial().parse(request.body);
        return app.prisma[this.model()].update({ where: { id }, data });
    }

    async delete(request: FastifyRequest, reply: FastifyReply) {
        const { id } = z.object({ id: z.string() }).parse(request.params);
        return app.prisma[this.model()].delete({ where: { id } });
    }
}